package com.happy.ibidding.controller;

import com.happy.ibidding.bean.Ask;
import com.happy.ibidding.bean.Bid;
import com.happy.ibidding.bean.News;
import com.happy.ibidding.service.NewsService;
import com.happy.ibidding.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("api/news")
public class NewsController {

    final private NewsService newsService;

    @Autowired
    public NewsController(NewsService newsService) {
        this.newsService = newsService;
    }


    @PostMapping
    public News saveNews(@RequestBody News news) {
        if (Objects.isNull(news.getId())) {
            news.setNewsId(RandomUtil.generateRandomId("news-", ""));
        }
        news.setEditTime(System.currentTimeMillis());
        return this.newsService.save(news);
    }

    @GetMapping("/all")
    public List<News> viewAll() {
        return this.newsService.viewAll();
    }

    @GetMapping("/{mongodbId}")
    public News view(@PathVariable String mongodbId) {
        return this.newsService.view(mongodbId);
    }

    @GetMapping("/delete/{mongodbId}")
    public void deleteNews(@PathVariable String mongodbId) {
        this.newsService.delete(mongodbId);
    }
}
